package com.platform.netty.tcp.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.platform.common.manager.AsyncManager;
import com.platform.common.utils.netty.NettyUtil;
import com.platform.common.utils.redis.RedisUtil;
import com.platform.common.utils.spring.SpringUtils;
import com.platform.module.domain.LinkConcentratorMeter;
import com.platform.module.domain.LinkConcentratorValve;
import com.platform.module.service.ILinkConcentratorMeterService;
import com.platform.module.service.ILinkConcentratorValveService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;
import java.util.TimerTask;

@Data
@Slf4j
public class CentraPoolTask {

    private static RedisUtil redisUtil;

    private static ILinkConcentratorValveService concentratorValveService;

    private static ILinkConcentratorMeterService concentratorMeterService;

    public static TimerTask saveCentra(final String datas, final int type)
    {
        concentratorValveService = SpringUtils.getBean(ILinkConcentratorValveService.class);

        concentratorMeterService = SpringUtils.getBean(ILinkConcentratorMeterService.class);

        redisUtil = SpringUtils.getBean(RedisUtil.class);

        return new TimerTask() {
            @Override
            public void run() {
                if(type==1){
                    //获取采集器关联设备
                    String[] msgs = datas.split(" ");
                    saveLink(msgs);
                }else if(type==2){
                    //采集器初始化
                    saveInit(datas);
                }
            }
        };
    }

    /**
     * 将采集到的设备关联信息存放到数据库
     */
    private static void saveLink(String[] datas){
        String clientId = datas[2] + datas[1];
        try {
            int datalength = NettyUtil.get10HexNum(datas[9]+ datas[10]);
            if(datalength==0){
                redisUtil.hset(clientId,"0C", "0", 10);
            }else {
                String msgs = NettyUtil.strChange(datas, 11, datalength);
                datas = msgs.split(" ");
                int len = datas.length / 5;
                String[] groups = new String[len];
                int x = 0;
                for (int i = 0; i < datas.length; i++) {
                    if (i != 0 && i % 5 == 0) {
                        x += 1;
                    }
                    if (groups[x] == null) {
                        groups[x] = "";
                    }
                    groups[x] += datas[i] + " ";
                }
                for (String group : groups) {
                    String[] contents = group.split(" ");
                    if (contents[4].equals("00")) {
                        String valveId = contents[3] + contents[2];
                        if(redisUtil.hHasKey("concentratorValve", valveId)){
                            LinkConcentratorValve concentratorValve = new LinkConcentratorValve();
                            concentratorValve.setValveId(valveId);
                            concentratorValve.setConcentratorId(clientId);
                            concentratorValve.setStatus("1");
                            concentratorValveService.updateLinkConcentratorValve(concentratorValve);
                        }else{
                            LinkConcentratorValve concentratorValve = new LinkConcentratorValve();
                            concentratorValve.setValveId(valveId);
                            concentratorValve.setConcentratorId(clientId);
                            concentratorValve.setStatus("1");
                            concentratorValveService.insertLinkConcentratorValve(concentratorValve);
                        }
                    } else {
                        String calorimeterId = contents[0] + contents[1] + contents[2] + contents[3];
                        if(redisUtil.hHasKey("concentratorMeter", calorimeterId)){
                            LinkConcentratorMeter concentratorMeter = new LinkConcentratorMeter();
                            concentratorMeter.setCalorimeterId(calorimeterId);
                            concentratorMeter.setConcentratorId(clientId);
                            concentratorMeter.setStatus("1");
                            concentratorMeterService.updateLinkConcentratorMeter(concentratorMeter);
                        }else{
                            LinkConcentratorMeter concentratorMeter = new LinkConcentratorMeter();
                            concentratorMeter.setCalorimeterId(calorimeterId);
                            concentratorMeter.setConcentratorId(clientId);
                            concentratorMeter.setStatus("1");
                            concentratorMeterService.insertLinkConcentratorMeter(concentratorMeter);
                        }
                    }
                }

                redisUtil.hset(clientId, "0C", Integer.toString(len), 10);
            }
        }catch (Exception e){
            e.printStackTrace();

            redisUtil.hset(clientId,"0C", "0", 10);

            AsyncManager.me().execute(AlarmPoolTask.saveAlarm(clientId, "3", "1", "获取采集器关联设备", "采集器"+clientId+"数据["+datas+"]有误，解析错误"+e.getMessage()));
        }
    }

    /**
     * 将阀门和热表登记为未登记状态
     * @param clientId
     */
    private static void saveInit(String clientId){
        List<LinkConcentratorMeter> linkConcentratorMeterList = concentratorMeterService.list(new QueryWrapper<LinkConcentratorMeter>().eq("concentrator_id", clientId));
        if(linkConcentratorMeterList!=null && linkConcentratorMeterList.size()>0){
            for(LinkConcentratorMeter linkConcentratorMeter : linkConcentratorMeterList){
                linkConcentratorMeter.setStatus("0");
                concentratorMeterService.updateLinkConcentratorMeter(linkConcentratorMeter);
            }
        }
        List<LinkConcentratorValve> linkConcentratorValveList = concentratorValveService.list(new QueryWrapper<LinkConcentratorValve>().eq("concentrator_id", clientId));
        if(linkConcentratorValveList!=null && linkConcentratorValveList.size()>0){
            for(LinkConcentratorValve linkConcentratorValve : linkConcentratorValveList){
                linkConcentratorValve.setStatus("0");
                concentratorValveService.updateLinkConcentratorValve(linkConcentratorValve);
            }
        }
    }
}
